// pages/card_detail/card_detail.js
const app = getApp();
Page({
  data: {
    cardDetail:[],
    checked: false,
    disabled: false,   
    read_checked:true,
    is_read:1,
    discounts_checked:0,
    is_discounts:0,
    userinfo:[],
    cost:0,
    discounts_money: 0
  },

  onLoad: function(options) {
    var that = this
    var id = options.id
    wx.request({
      url: app.url + 'weiapp/api/getCardDetail',
      data: {
        id: id
      },
      success: function(res) {
        console.log(res)
        if (res.data.code != 200) {
          wx.showToast({
            title: '错误代码:' + res.data.code,
            icon: 'none',
            duration: 5000
          })
        }
        else
        {
          that.setData({
            cardDetail:res.data.data,
            cost:res.data.data.money
          })
        }
      }
    }),
    wx.request({
      url:app.url + "/weiapp/api/userinfo",
      data:{
        uid:wx.getStorageSync('uid')
      },
      success:function(res)
      {
        that.setData({
          userinfo:res.data.data
        })
        if(!res.data.data.mobile)
        {
          wx.showToast({
            title: '请补充完成电话号码',
          })
          setTimeout(function(){
            wx.navigateTo({
              url: '../sms/sms',
            })

          }
          ,2000)
        }
      }
    })
  },
  listenerRead:function(e) {
    console.log(e)
    if(this.data.read_checked == true)
    {
      this.setData({
        read_checked: false,
        is_read:0
      })
      console.log(this.data.is_read)
    }
    else
    {
      this.setData({
        read_checked: true,
        is_read: 1
      })
      console.log(this.data.is_read)      
    }
  },
  listenerDiscounts: function (e) {
    console.log(e)
    if (this.data.discounts_checked == true) {
      this.setData({
        discounts_checked: false,
        cost:this.data.cardDetail.money,
        discounts_money:0
      })
    }
    else {
      this.setData({
        discounts_checked: true
      })
      if (this.data.userinfo.money >= this.data.cardDetail.money)//如果用户金币大于购买金额
      {
        this.setData({
          discounts_money : this.data.cardDetail.money - 1,//设置抵扣金额等于购买金额
          cost : 1 //实际支付金额为1  必须要支付1元
        })
      }
      else {
        this.setData({
          discounts_money : this.data.userinfo.money, // 抵扣金额等于用户现有的金币
          cost : this.data.cardDetail.money - this.data.userinfo.money//设置抵扣金额等于会员卡金额减去现有金币
        })
      }
    }

  },  
  nowBuy(event)
  {
    console.log(event)
    if(this.data.is_read == false)
    {
      wx.showToast({
        title: '请阅读会员通告后在进行购买',
      })
      return;
    }
    const url = '../payment/payment?id=' + event.currentTarget.dataset.pid + '&pname=' + event.currentTarget.dataset.pname + '&money=' + event.currentTarget.dataset.money + '&img=' + event.currentTarget.dataset.img + '&cost=' + event.currentTarget.dataset.cost + '&discounts_money=' + event.currentTarget.dataset.discounts_money;//money是微信支付的钱  cost是该订单总消费 discounts_money是金币抵扣
    console.log(url)
    wx.navigateTo({
      url: url,
    })
  }
})